﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;


namespace IHISCETeim.DAL
{
    public  class WOITMDAL
    {
        /// <summary>
        /// 入库操作，可支持批量入库
        /// </summary>
        /// <param name="ItemCodes">多物料请使用半角逗号隔开</param>
        /// <param name="OperateTypes">请使用半角逗号隔开</param>
        /// <param name="Quantitys">请使用半角逗号隔开</param>
        /// <param name="OperateUser"></param>
        /// <param name="OperateDocNums">请使用半角逗号隔开</param>
        /// <param name="OperateRemarks">请使用半角逗号隔开</param>
        /// <returns>-1表示失败,1表示成功</returns>
        public static int AddInputStockDetail(string ItemCodes, string OperateTypes, string Quantitys, string OperateUser, string OperateDocNums, string OperateRemarks)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_AddInputStockDetail", ItemCodes, OperateTypes, Quantitys, OperateUser, OperateDocNums, OperateRemarks);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        /// <summary>
        /// 出库操作，可支持批量出库操作
        /// </summary>
        /// <param name="ItemCodes">多物料请使用半角逗号隔开</param>
        /// <param name="OperateTypes">请使用半角逗号隔开</param>
        /// <param name="Quantitys">请使用半角逗号隔开</param>
        /// <param name="OperateUser"></param>
        /// <param name="OperateDocNums">请使用半角逗号隔开</param>
        /// <param name="OperateRemarks">请使用半角逗号隔开</param>
        /// <returns>-1表示失败,1表示成功</returns>
        public static int AddOutStockDetail(string ItemCodes, string OperateTypes, string Quantitys, string OperateUser, string OperateDocNums, string OperateRemarks)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_AddOutStockDetail", ItemCodes, OperateTypes, Quantitys, OperateUser, OperateDocNums, OperateRemarks);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 根据物料号获取物料信息
        /// </summary>
        /// <param name="ItemCode"></param>
        /// <returns></returns>
        public static IDataReader GetByItemCode(string ItemCode)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_GetByItemCode", ItemCode);
            return DB.ExecuteReader(dbCommand);
	    }
        /// <summary>
        /// 获取物料出入库明细
        /// </summary>
        /// <param name="SQLWhere"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTable GetDetailPagination(string SQLWhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_GetDetailPagination");
            DB.AddInParameter(dbCommand, "SQLWhere", DbType.String,SQLWhere);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount= Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        /// <summary>
        /// 获取物料列表
        /// </summary>
        /// <param name="SQLWhere"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTable GetPagination(string SQLWhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_GetPagination");
            DB.AddInParameter(dbCommand, "SQLWhere", DbType.String,SQLWhere);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount= Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        /// <summary>
        /// 更改物料库位，工位
        /// </summary>
        /// <param name="ItemCode"></param>
        /// <param name="U_KW"></param>
        /// <param name="U_GW"></param>
        /// <returns></returns>
        public static int Update(WOITMInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("WOITM_Update", obj.ItemCode, obj.U_KW, obj.U_GW_55NSL01C, obj.U_GW_65NS001C, obj.U_GW_65NSL01C, obj.U_GW_80NS001C, obj.U_GW_100NS001C, obj.U_GW_100NST01C,obj.U_GW_40NS001C);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

       
    }
}
